package com.gaul.listener;

import com.gaul.action.Action;
import com.gaul.util.XmlParser;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.util.Map;

public class ActionListener implements ServletContextListener {
	public void contextDestroyed(ServletContextEvent event) {
		System.out.println("context destroy");
	}

	public void contextInitialized(ServletContextEvent event) {
		System.out.println("context init");
		
		//获取ServletContext对象
		ServletContext context = event.getServletContext();
		
		//获取xml配置文件路径
		String xmlPath = context.getInitParameter("springMVC");
		System.out.println(xmlPath);
//		xmlPath = context.getRealPath("\\") + xmlPath;
		InputStream xmlStream = this.getClass().getResourceAsStream("/" + xmlPath);

		//读取xml配置
		Map<String, Action> xmlMap = XmlParser.parseXml(xmlStream);
		
		//放入application作用于
		context.setAttribute("xmlMap", xmlMap);
	}
}
